<?php
namespace app\api\controller\amount;


class Curl
{
    //跨域请求
    public function curlRequest($config, $request, $json = true)
    {
        $url = $config['url'];          // 请求路径
        $method = $config['method'];    // 请求方法
        $headers = $config['headers'];  // 请求头部
        $body = $json ? json_encode($request) : $request;  // 请求主体
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_HEADER, 0);//返回response头部信息
        curl_setopt($curl, CURLOPT_FAILONERROR, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_POST, true); // 发送一个常规的Post请求
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $body);
        $result = curl_exec($curl);
        $header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
        $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
        $rbody = substr($result, $header_size);
        curl_close($curl);
        return [
            'body' => $result,
            'httpCode' => $httpCode
        ];
    }
}